# Maintainer: Nazar Mishturak <nazar m x at gmail dot com>

_realname=glbinding
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
conflicts=("${MINGW_PACKAGE_PREFIX}-angleproject")
pkgver=3.1.0
pkgrel=3
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
url='https://github.com/cginternals/glbinding'
pkgdesc="A C++ binding for the OpenGL API, generated using the gl.xml specification (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
             # "${MINGW_PACKAGE_PREFIX}-doxygen"
license=('MIT')
options=('strip' 'staticlibs' 'docs')
source=(${_realname}-${pkgver}.tar.gz::"https://github.com/cginternals/glbinding/archive/v${pkgver}.tar.gz"
        'mingw-unix-layout.patch'
        'config-export-location.patch'
        'fix-proc-address.patch')
sha256sums=('6729b260787108462ec6d8954f32a3f11f959ada7eebf1a2a33173b68762849e'
            '57880396bc5b40831f01bf95d0c2c976c722e181b7f01734c4a3d0dabcbb1e77'
            '3340339a2f5c1c0c660d169dea964579bc3f203c08ba81f10fce4406604f3e1a'
            '1cdb0febf6ef434f664dd13b3cb718971609191d69debe2924b74b5fe8bf87f1')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}/mingw-unix-layout.patch"
  patch -p1 -i "${srcdir}/config-export-location.patch"
  patch -p1 -i "${srcdir}/fix-proc-address.patch"
}

build() {
  local BUILD_TYPE="Release"
  if check_option "debug" "y"; then
    BUILD_TYPE="Debug"
  fi

  [[ -d ${srcdir}/build-${MSYSTEM} ]] && rm -rf ${srcdir}/build-${MSYSTEM}
  mkdir -p ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DOPTION_BUILD_EXAMPLES=OFF \
    -DOPTION_BUILD_TOOLS=OFF \
    -DOPTION_BUILD_TESTS=OFF \
    -DBUILD_SHARED_LIBS=ON \
    -DOPTION_BUILD_DOCS=OFF \
    -DSYSTEM_DIR_INSTALL=ON \
    ../${_realname}-${pkgver}

  cmake --build .
}

package () {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR=${pkgdir} cmake --install .

  # Remove KHR include, fixes conflict with mingw-w64
  rm -rf "${pkgdir}${MINGW_PREFIX}"/include/KHR

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
